/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jhu.profiteer.data;

import java.io.Serializable;
import java.util.Random;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import jhu.profiteer.data.exceptions.PreexistingEntityException;

/**
 *
 * @author Jeremy
 */
@Entity
public class LocationCommodity implements Serializable {
  private static final long serialVersionUID = 1L;
  private LocationCommodityPK id;
  private Long minPrice;
  private Long maxPrice;

  @Override
  public int hashCode() {
    int hash = 0;
    hash += (null != getId() ? getId().hashCode() : 0) +
            (null != minPrice ? minPrice.hashCode() : 0) +
            (null != maxPrice ? maxPrice.hashCode() : 0);
    return hash;
  }

  @Override
  public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof LocationCommodity)) {
      return false;
    }
    LocationCommodity other = (LocationCommodity) object;
    if ((null == this.getId() && null != other.getId()) ||
        (null != this.getId() &&
         !this.id.equals(other.id))) {
      return false;
    }
    if ((null == this.minPrice && null != other.minPrice) ||
        (null != this.minPrice && !this.minPrice.equals(other.minPrice))) {
      return false;
    }
    if ((null == this.maxPrice && null != other.maxPrice) ||
        (null != this.maxPrice && !this.maxPrice.equals(other.maxPrice))) {
      return false;
    }
    return true;
  }

  @Override
  public String toString() {
    return "jhu.profiteer.data.LocationCommodity[id=" + getId() +
           ", minPrice=" + minPrice +
           ", maxPrice=" + maxPrice + "]";
  }

  /**
   * @return the minPrice
   */
  public Long getMinPrice() {
    return minPrice;
  }

  /**
   * @param minPrice the minPrice to set
   */
  public void setMinPrice(Long minPrice) {
    this.minPrice = minPrice;
  }

  /**
   * @return the maxPrice
   */
  public Long getMaxPrice() {
    return maxPrice;
  }

  /**
   * @param maxPrice the maxPrice to set
   */
  public void setMaxPrice(Long maxPrice) {
    this.maxPrice = maxPrice;
  }

  public void generateCurrentValue(Character c)
          throws PreexistingEntityException, Exception
  {
    CurrentCommodityValuePK ccvpk = new CurrentCommodityValuePK(c.getId(),
            this.getId().getCommodityId());
    CurrentCommodityValue ccv = new CurrentCommodityValue();
    ccv.setId(ccvpk);
    Random r = new Random();
    int range = this.maxPrice.intValue() - this.minPrice.intValue() + 1;
    Long price = r.nextInt(range) + this.minPrice;
    ccv.setPrice(price);
    new CurrentCommodityValueJpaController().create(ccv);
  }

  /**
   * @return the id
   */
  @EmbeddedId
  public LocationCommodityPK getId() {
    return id;
  }

  /**
   * @param id the id to set
   */
  public void setId(LocationCommodityPK id) {
    this.id = id;
  }
}
